import request from '@/config/axios'

// 账户余额 VO
export interface AccountBalanceVO {
  id: number // 主键ID
  merchantId: number // 商家ID
  accountType: number // 账户类型
  accountId: string // 子账户ID
  balance: number // 账户余额
  frozenAmount: number // 冻结金额
  status: number // 状态
  rechargeDiscountType?: number // 充值折扣类型
  rebateRatio?: number // 返利比例（百分比值，如5表示5%）
}

// 商家信息 VO
export interface MerchantVO {
  id: number // 主键ID
  merchantName: string // 商家名称
  contactPerson: string // 联系人
  phone: string // 联系电话
  email: string // 电子邮箱
  address: string // 地址
  status: number // 状态
}

// 账户余额 API
export const AccountBalanceApi = {
  // 查询账户余额分页
  getAccountBalancePage: async (params: any) => {
    return await request.get({ url: `/juliang/account-balance/page`, params })
  },

  // 查询账户余额详情
  getAccountBalanceById: async (id: number) => {
    return await request.get({ url: `/juliang/account-balance/getById?id=` + id })
  },

  // 新增账户余额
  createAccountBalance: async (data: AccountBalanceVO) => {
    return await request.post({ url: `/juliang/account-balance/create`, data })
  },

  // 修改账户余额
  updateAccountBalance: async (data: AccountBalanceVO) => {
    return await request.post({ url: `/juliang/account-balance/update`, data })
  },

  // 修改账户余额状态
  updateAccountBalanceStatus: async (id: number, status: number) => {
    const data = {
      id,
      status
    }
    return await request.post({ url: `/juliang/account-balance/updateStatus`, data: data })
  },

  // 删除账户余额
  deleteAccountBalanceById: async (id: number) => {
    return await request.get({ url: `/juliang/account-balance/delete?id=` + id })
  },

  // 导出账户余额 Excel
  exportAccountBalance: async (params) => {
    return await request.download({ url: `/juliang/account-balance/export-excel`, params })
  },

  // 获取商家列表（分页）
  getMerchantPage: async (params: {
    pageNo: number
    pageSize: number
    merchantName?: string
  }) => {
    return await request.get({ 
      url: '/juliang/merchant/page', 
      params: {
        pageNo: params.pageNo,
        pageSize: params.pageSize,
        merchantName: params.merchantName
      }
    })
  },

  // 发送催款通知
  sendDunningNotice: async (data: {
    accountId: number
    merchantId: number
    dunningType: number
    content: string
    balance: number
  }) => {
    return await request.post({ url: `/juliang/account-balance/sendDunning`, data })
  },

  // 获取聚亮交易记录
  getJuliangTransactionRecord: async (params: any) => {
    return await request.get({ url: `/juliang/account-balance/getJuliangTransactionRecord`, params })
  },

  // 获取聚亮资金日统计流水
  getJuliangFundDailyStatFlow: async (params: any) => {
    return await request.get({ url: `/juliang/account-balance/getJuliangFundDailyStatFlow`, params })
  },

  // 获取聚亮余额
  getJuliangBalance: async (params: any) => {
    return await request.get({ url: `/juliang/account-balance/getJuliangBalance`, params })
  },

  // 转账
  transferBalance: async (data: {
    fromAccountId: number
    toAccountId: number
    amount: number
    remark?: string
  }) => {
    return await request.post({ url: `/juliang/account-balance/transferBalance`, data })
  },

  // 获取可转账账户列表
  getTransferableAccounts: async (params: {
    pageNo: number
    pageSize: number
    queryBeikuanAccount: boolean
    excludeAccountId?: number
  }) => {
    return await request.get({ url: `/juliang/account-balance/page`, params })
  },

  // 获取转账记录分页
  getTransferRecordPage: async (params: {
    pageNo: number
    pageSize: number
    merchantId?: number
    fromAccountId?: number
    toAccountId?: number
    startTime?: string
    endTime?: string
  }) => {
    return await request.get({ url: `/juliang/account-balance/getTransferRecordPage`, params })
  },

  // 转账审核
  transferBalanceApplyAudit: async (data: {
    id: number
    status: number // 1审核成功 2审核失败
    remark?: string
  }) => {
    return await request.post({ url: `/juliang/account-balance/transferBalanceApplyAudit`, data })
  }
}
